package com.yumis.service.modules.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.exception.GlobalException;
import com.yumis.service.common.utils.CodecUtils;
import com.yumis.service.common.vo.Page;
import com.yumis.service.common.vo.Response;
import com.yumis.service.model.pojo.Staff;
import com.yumis.service.modules.user.mapper.StaffMapper;
import com.yumis.service.modules.user.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffMapper mapper;

    @Override
    public void register(Staff staff) {
        int currTime = (int) (System.currentTimeMillis()/1000);
        staff.setId(null);
        staff.setCreateTime(currTime);
        staff.setUpdateTime(currTime);
        if(staff.getPassword() == null || staff.getPassword().equals("")){
            staff.setPassword("123456");
        }

        //生成盐
        String salt = CodecUtils.generateSalt();
        staff.setSalt(salt);
        //生成密码
        String md5Pwd = CodecUtils.md5Hex(staff.getPassword(), staff.getSalt());
        staff.setPassword(md5Pwd);
        //保存到数据库
        int count = mapper.insert(staff);
        if (count != 1) {
            throw new GlobalException(Response.buildResponse(HttpState.CREATED_FAILED));
        }
    }

    @Override
    public Staff login(String phone, String password) {
        Staff record = new Staff();
        record.setPhone(phone);
        Staff user = mapper.selectOne(record);
        log.info("[用户查询] {}",user);
        if (user == null) {
            throw new GlobalException(Response.buildResponse(HttpState.USER_NOT_FOUND));
        }
        log.info("[密码对比] {}，{}", CodecUtils.md5Hex(password, user.getSalt()),user.getPassword());
        //检验密码是否正确
        if (!CodecUtils.md5Hex(password, user.getSalt()).equals(user.getPassword())) {
            //密码不正确
            throw new GlobalException(Response.buildResponse(HttpState.PASSWORD_ERROR));
        }
        return user;
    }

    @Override
    public Page<Staff> queryStaffs(Integer page, Integer rows) {
        //分页
        PageHelper.startPage(page,rows);
        //过滤
        Example example = new Example(Staff.class);
        example.setOrderByClause("create_time desc");
        //查询
        List<Staff> list = mapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        //解析分页结果
        PageInfo<Staff> info = new PageInfo<>(list);
        return new Page<>(info.getTotal(),info.getPages(),list);
    }

    @Override
    public void editStaffs(Staff staff) {
        staff.setUpdateTime((int) (System.currentTimeMillis()/1000));
        int count = mapper.updateByPrimaryKeySelective(staff);
        if(count != 1){
            throw new GlobalException(Response.buildResponse(HttpState.UPDATE_ERROR));
        }
    }

    @Override
    public Page<Staff> queryStaffsByPage(Integer page, Integer rows, String key) {
        //分页
        PageHelper.startPage(page,rows);
        //过滤
        Example example = new Example(Staff.class);
        example.setOrderByClause("create_time desc");
        if(StringUtils.isNoneBlank(key)){//过滤条件
            example.createCriteria().orLike("realName","%" + key + "%");
        }
        //查询
        List<Staff> list = mapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        //解析分页结果
        PageInfo<Staff> info = new PageInfo<>(list);
        return new Page<>(info.getTotal(),info.getPages(),list);
    }

}
